<template>
  <div class="bg_write">
    <tableComponents
      :isShow="false"
      @toAdd="openDio"
      :isHaveExport="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          header-row-class-name="head_color"
          v-loading="loading"
          stripe
          tooltip-effect="dark"
          style="width: 99.9%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="奖励名称" min-width="150">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="奖励标识iden" min-width="150">
            <template slot-scope="scope">{{ scope.row.iden }}</template>
          </el-table-column>

          <el-table-column label="类型" min-width="100">
            <template slot-scope="scope">{{ scope.row.types_cn }}</template>
          </el-table-column>

          <el-table-column label="所属模块" min-width="150">
            <template slot-scope="scope">{{ scope.row.plugin_cn || '---' }}</template>
          </el-table-column>

          <el-table-column label="更新时间" min-width="150">
            <template slot-scope="scope">{{ scope.row.update_time | getTimeFormat }}</template>
          </el-table-column>

          <el-table-column label="是否发消息" min-width="150">
            <template slot-scope="scope">
              <switchc
                :value="scope.row.is_sms"
                @change="IsSendSmS($event,scope.row.id)"
                text="on|off"
              ></switchc>
            </template>
          </el-table-column>

          <el-table-column label="是否显示" min-width="150">
            <template slot-scope="scope">
              <switchc
                :value="scope.row.show"
                @change="handleSwitch($event,scope.row.id)"
                text="on|off"
              ></switchc>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="showEditDialog(scope.$index,scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="rewardId ?'编辑奖励名称':'添加奖励名称'"
      :modal-append-to-body="false"
      :visible.sync="dialogFormVisible"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" label-width="120px">
        <el-form-item label="奖励名称" prop="title">
          <el-col :span="12">
            <el-input placeholder="请输入奖励名称" v-model="data.title" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="奖励标识" prop="iden">
          <el-col :span="12">
            <el-input
              placeholder="请输入奖励标识"
              v-model="data.iden"
              :disabled="rewardId ? true: false"
              clearable
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="类型" prop="types">
          <el-col :span="12">
            <el-select
              v-model="data.types"
              placeholder="请选择"
              @change="handChange"
              style="width:100%"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="rewardId ? true: false"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="所属类目" prop="types">
          <el-col :span="12">
            <el-select
              v-model="data.plugin"
              placeholder="请选择"
              @change="handChange"
              style="width:100%"
            >
              <el-option
                v-for="item in rewarPlugin"
                :key="item.iden"
                :label="item.title"
                :value="item.iden"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        
        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addSave('data')"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>

import switchc from "@/components/currency/Swich";
export default {
  data() {
    var checkTitle = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标题不能为空"));
      } else {
        callback();
      }
    };
    var checkIden = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标识不能为空"));
      } else {
        callback();
      }
    };
    return {
      list: [],
      rewardId: "",
      dialogFormVisible: false,
      uddateIndex: "",
      options: [
        {
          value: "",
          label: ""
        }
      ],
      value: "",
      data: {
        iden: "",
        title: "",
        types: "0",
        plugin:'',
        plugin_cn:''
      },
      loading: false,
      dataYZ: {
        title: [{ validator: checkTitle, trigger: "blur" }],
        iden: [{ validator: checkIden, trigger: "blur" }]
      },
      rewarPlugin:[]
    };
  },
  components: {
    switchc
  },

  methods: {
    getRewardPlugin() {
      this.$api.getPlugin.getRewardPlugin({}).then(res => {
        if (res.data.code == 200) {
          this.rewarPlugin=res.data.result;
        }
      });
    },
    openDio() {
      this.dialogFormVisible = true;
      this.rewardId = "";
      this.data = {
        iden: "",
        title: "",
        types: "0"
      };
    },
    handChange(val) {
      this.data.types = val;
    },
    showEditDialog(index, row) {
      this.rewardId = row.id;
      this.uddateIndex = index;
      this.dialogFormVisible = true;
      this.data = row;
    },
    getTypesList() {
      var data = {};
      this.$api.getAdminuser.getRewardTypes(data).then(res => {
        this.options = res.data.result;
      });
    },
    getNewsAll() {
      this.loading = true;
      var data = {};
      this.$api.getAdminuser.getRewardList(data).then(res => {
        if (res.data.code) {
          this.list = res.data.result;
        }
      });
      this.loading = false;
    },
    IsSendSmS(val, id) {
      var params = {};
      params["id"] = id;
      val == true ? (params["is_sms"] = 1) : (params["is_sms"] = 0);
      this.$api.getAdminuser.IsSendSmS(params).then(res => {
        if (res.data.status == 0) {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    handleSwitch(val, id) {
      var params = {};
      params["id"] = id;
      val == true ? (params["show"] = 1) : (params["show"] = 0);
      this.$api.getAdminuser.changeRewardShow(params).then(res => {
        if (res.data.status == 0) {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id };
          this.$api.getAdminuser.RewardDelete(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              // this.getNewsAll("");
              this.list.splice(index, 1);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    handleSelectionChange() {},
    addSave(formName) {
      var _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["iden"] = this.data.iden;
          params["types"] = this.data.types;
          params["title"] = this.data.title;
          if (this.rewardId) {
            this.$api.getAdminuser.rewardUpdate(this.data).then(res => {
              if (res.data.code == 200 && res.data.result != null) {
                this.$fnc.alertSuccess({ text: "修改成功" }); //成功弹出框
                this.dialogFormVisible = false;
                _this.data.update_time = new Date().getTime();
                _this.list[this.uddateIndex] = _this.data;
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
              }
            });
          } else {
            this.$api.getAdminuser.rewardSave(params).then(res => {
              if (res.data.code == 200 && res.data.result != null) {
                this.$fnc.alertSuccess({ text: "添加成功" }); //成功弹出框
                this.dialogFormVisible = false;

                _this.data.created_time = new Date().getTime();
                _this.data.update_time = new Date().getTime();
                _this.list.push(_this.data);
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
              }
            });
          }
        } else {
          return false;
        }
      });
    }
  },
  beforeCreate() {
    this.loading = true; //数据未加载之前调用loading
  },
  created() {
    this.getNewsAll();
    this.getTypesList();
    this.loading = false;
    this.getRewardPlugin()
  }
};
</script>

